package com.tenebrous.jestic.chain;

import com.alibaba.fastjson.JSON;
import com.tenebrous.jestic.compents.exception.BizException;
import com.tenebrous.jestic.context.JesticContext;
import com.tenebrous.jestic.context.JesticTask;
import com.tenebrous.jestic.context.Response;
import com.tenebrous.jestic.entity.model.request.JesticAuthorityRequest;
import com.tenebrous.jestic.entity.model.response.JesticAuthorityResponse;
import com.tenebrous.jestic.enums.TaskStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Project: jestic-spring-boot
 * @Package: com.zy.tenebrous.jestic.chain
 * @Author: zhangya
 * @CreateTime: 2023-06-28  18:15
 * @Version: 1.0.0
 */
@Slf4j
@Service("JesticAuthVerificationHandler")
public class JesticAuthVerificationHandler extends AbstractJesticChainHandler<JesticAuthorityRequest, JesticAuthorityResponse> {

    public JesticAuthVerificationHandler() {
        super();
    }

    private  int order;

    @Override
    public void handler(JesticContext<? extends JesticTask, ? extends Response> context) {

        JesticAuthorityRequest request = (JesticAuthorityRequest) context.getRequest();
        log.info("JesticAuthVerificationHandler.handler.start.request[{}]", JSON.toJSONString(request));
        JesticAuthorityResponse response = (JesticAuthorityResponse) context.getResponse();
        String serverName = request.getServerName();
        String serverId = request.getServerId();
        if (StringUtils.isBlank(serverName) || StringUtils.isBlank(serverId)) {
            response.setStatus(TaskStatus.TERMINATED);
            throw new BizException(500, "校验失败");
        }
        if (null != nextHandler) {
            nextHandler.handler(context);
        }
    }

    @Override
    public int order(String taskType) {
        return this.order;
    }

    @Override
    public int getOrder() {
        return this.order;
    }

    @Override
    public AbstractJesticChainHandler<JesticAuthorityRequest, JesticAuthorityResponse> setOrder(int order) {
        return null;
    }


}
